<?php
class Silk_Zgb_Helper_Attribute extends Mage_Core_Helper_Abstract
{
	public function getDb()
	{
        return Mage::getSingleton('core/resource')->getConnection('core_read');
	}

	public function getAttributeIdByCode($code, $type_id = 4)
	{
		$sql = "select attribute_id from eav_attribute where attribute_code='{$code}' and entity_type_id='{$type_id}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getAttributeCodeById($id, $type_id = 4)
	{
		$sql = "select attribute_code from eav_attribute where attribute_id='{$id}' and entity_type_id='{$type_id}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getAttributeOptionByCode($code)
	{
		$attributeId = $this->getAttributeIdByCode($code);
		$sql = "SELECT eaov.`option_id` as `value`,eaov.`value` as label FROM `eav_attribute_option` eao LEFT JOIN `eav_attribute_option_value` eaov ON eao.`option_id`=eaov.`option_id` WHERE eao.`attribute_id`='{$attributeId}' ORDER BY CONVERT( eaov.`value` USING gbk ) COLLATE gbk_chinese_ci ASC";
    	$query = $this->getDb()->query($sql);
    	return $query->fetchAll();
	}

	public function getOptionIdByLabel()
	{

	}

	public function getStoreCode($name)
	{
		$sql = "SELECT `code` FROM core_store WHERE `name`='{$name}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getWebsiteCode($name)
	{
		$sql = "SELECT `code` FROM core_website WHERE `name`='{$name}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getManufacturerId($name)
	{
		$sql = "SELECT id FROM `zgb_manufacturer` WHERE `name`='{$name}'";
		
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getDealerId($name)
	{
		$sql = "SELECT id FROM `zgb_dealer` WHERE `name`='{$name}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}

	public function getWarehouseId($name)
	{
		$sql = "SELECT id FROM `zgb_warehouse` WHERE `name`='{$name}'";
		$query = $this->getDb()->query($sql);
		return $query->fetchColumn();
	}
}